package pl.com.like.sequoia.service.impl;

import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import pl.com.like.sequoia.dao.admin.IConfigDao;
import pl.com.like.sequoia.exceptions.LogicalException;
import pl.com.like.sequoia.model.admin.IConfig;
import pl.com.like.sequoia.model.bio.enums.ConfigType;
import pl.com.like.sequoia.service.IConfigService;

public class ConfigService implements IConfigService {

	private final Logger log = LoggerFactory.getLogger(ConfigService.class);
	private final String defaultTempDir = "java.io.tmpdir";
	@Autowired
	private IConfigDao configDao;

	/** {@inheritDoc} */
	public String getBasePath() throws LogicalException {
		IConfig pathConfig = configDao.get(ConfigType.FILE_DIR);
		String systemPath = null;
		if (null != pathConfig) {
			systemPath = pathConfig.getValue();
			systemPath = systemPath.trim();
			if ((null == systemPath) || (systemPath.length() == 0)) {
				throw new LogicalException("System download path is not set in database");
			}
		} else {
			throw new LogicalException("System download path is not set in database");
		}
		return systemPath;
	}

	public IConfigDao getConfigDao() {
		return configDao;
	}

	public String getPdbSiteFormat() throws LogicalException {
		IConfig pathConfig = configDao.get(ConfigType.PDB_SITE);
		String pdbSiteUrl = null;
		String errorMsg = "System download path is not set in database";
		if (null != pathConfig) {
			pdbSiteUrl = pathConfig.getValue();
			if (null != pdbSiteUrl) {
				pdbSiteUrl = pdbSiteUrl.trim();
				if (pdbSiteUrl.length() == 0) {
					throw new LogicalException(errorMsg);
				}
			} else {
				throw new LogicalException(errorMsg);
			}
		} else {
			throw new LogicalException("Online repository for PDB files is not set");
		}
		return pdbSiteUrl;
	}

	/**
	 * {@inheritDoc}
	 */
	public String getUploadPath() {
		IConfig uploadConfig = configDao.get(ConfigType.UPLOAD);
		String uploadPath = null;
		if (null == uploadConfig) {
			uploadPath = System.getProperty(defaultTempDir);
			log.warn("Could not get from database temporary upload directory. Take default temporary directory {}",
					uploadPath);
			if (null == uploadPath) {
				throw new LogicalException("Could not get upload directory from database, even also could not get"
						+ "system default temporary directory. Please set in application upload directory");
			}
		} else {
			uploadPath = uploadConfig.getValue();
		}
		uploadPath = uploadPath.trim();
		if (!uploadPath.isEmpty()) {
			File tempFile = new File(uploadPath);
			if (!tempFile.isDirectory()) {
				throw new LogicalException("given path is not a path to directory. Please set directory path"
						+ " for upload directory application's value.");
			}
		} else {
			throw new LogicalException("set upload directory is incorrect. should be set existing dir path, "
					+ "was set empty string");
		}
		return uploadPath;
	}

	public void setConfigDao(IConfigDao configDao) {
		this.configDao = configDao;
	}
}
